package com.quickly.develop.common.crud;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.quickly.develop.common.base.BaseDao;
import com.quickly.develop.common.base.BaseService;

/**
 * Service基类
 * 
 * @author qyd
 * @version 2020-05-10
 */
@Transactional(readOnly = true)
public abstract class CrudService<T extends DataEntity, D extends CrudDao<T>> implements BaseService<T,BaseDao> {

	/**
	 * 持久层对象
	 */
	@Autowired
	protected D dao;

	/**
	 * 获取单条数据，主键UUID
	 * 
	 * @param id
	 * @return
	 */
	public T get(String id) {
		return dao.get(id);
	}

	/**
	 * 获取单条数据，主键Long自增长
	 * 
	 * @param id
	 * @return
	 */
	public T get(Long id) {
		return dao.get(id);
	}

	/**
	 * 获取单条数据，条件查询
	 * 
	 * @param entity
	 * @return
	 */
	public T get(T entity) {
		return dao.get(entity);
	}

	/**
	 * 保存一条
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public int save(T entity) {
		return dao.save(entity);
	}

	/**
	 * 保存多条
	 * 
	 * @param entity
	 * @return
	 */
	@Transactional(readOnly = false)
	int saveList(List<T> list) {
		return dao.saveList(list);
	}

	/**
	 * 修改一条
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public int update(T entity) {
		return dao.update(entity);
	}

	/**
	 * 修改多条
	 * 
	 * @param entity
	 * @return
	 */
	@Transactional(readOnly = false)
	int updateList(List<T> list) {
		return dao.updateList(list);
	}

	/**
	 * 删除一条，主键UUID
	 * 
	 * @param entity
	 * @return
	 */
	int delete(String id) {
		return dao.delete(id);
	}

	/**
	 * 删除一条，主键Long自增长
	 * 
	 * @param entity
	 * @return
	 */
	int delete(Long id) {
		return dao.delete(id);
	}

	/**
	 * 删除数据,条件删除
	 * 
	 * @param entity
	 */
	@Transactional(readOnly = false)
	public void delete(T entity) {
		dao.delete(entity);
	}
	/**
	 * 删除多条
	 * @param entity
	 * @return
	 */
	@Transactional(readOnly = false)
	int deleteList(List<T> list) {
		return	dao.deleteList(list);
	}
	
	
	
	
	/**
	 * 查询总数
	 * 
	 * @param entity
	 * @return
	 */
	int countByEntity(T entity) {
		return dao.countByEntity(entity);
	}

	/**
	 * 查询数据列表，设置分页;
	 * 
	 * @param entity
	 * @param offset
	 * @param limit
	 * @return
	 */
	public List<T> queryPageByEntity(T entity, Integer offset, Integer limit) {
		List<T> list = dao.queryPageByEntity(entity, offset, limit);
		return list;
	}

	/**
	 * 查询数据列表，设置所有;
	 * 
	 * @param entity
	 * @return
	 */
	List<T> queryAllByEntity(T entity) {
		List<T> list = dao.queryAllByEntity(entity);
		return list;
	}

	/**
	 * 查询总数
	 * 
	 * @param entity
	 * @return
	 */
	public int countAllByMap(Map<String, Object> params) {
		return dao.countAllByMap(params);
	}

	/**
	 * 查询分页数据
	 * 
	 * @param params
	 * @return
	 */
	public List<T> queryAllByMap(Map<String, Object> params) {
		List<T> list = dao.queryAllByMap(params);
		return list;
	}

	/**
	 * 查询分页数据
	 * 
	 * @param params
	 * @param offset
	 * @param limit
	 * @return
	 */
	public List<T> queryPageByMap(Map<String, Object> params, Integer offset, Integer limit) {

		List<T> list = dao.queryPageByMap(params, offset, limit);
		return list;
	}

}
